package com.kaikeba.wechat.filter;

import com.kaikeba.bean.Courier;
import com.kaikeba.bean.User;
import com.kaikeba.service.UserService;
import com.kaikeba.util.UserSessionUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"/index.html"})
public class WeChatFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setContentType("text/html;charset=utf-8");
        User user = UserSessionUtil.getUser(req.getSession());
        Courier courier = UserSessionUtil.getCourier(req.getSession());
        if (user!=null||courier!=null){
            chain.doFilter(request, response);
        }else {
            resp.sendRedirect("/login.html");
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
